package com.hzit.user.controller;

import cn.hutool.crypto.SecureUtil;
import com.hzit.user.entity.User;
import com.hzit.user.service.IUserService;
import com.hzit.user.vo.UserVo;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wf
 * @since 2023-09-14
 */
@Controller
public class UserController {
	@Autowired
	private IUserService userService;

	//1. 欢迎页面
	@GetMapping({"/","/register.html"})
	public String register(){
		// XxlSsoUser xxlUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
		// model.addAttribute("username",xxlUser.getUsername());
		return "register";
	}

	//2. 获取手机号，生成验证码并存放到redis中
	@GetMapping("/user/getCode")
	public String createCode(String phone){
		userService.createCode(phone);
		return "register";
	}

	//3. 保存用户
	@PostMapping("/user/save")
	public String save(UserVo vo) throws InvocationTargetException, IllegalAccessException {
		//3.1 比较redis的验证码跟这里的验证码是否一致
		boolean b = userService.compareCode(vo.getCode(),vo.getPhone());
		if(b){  //3.2 如果比较成功才添加到数据库中
			//3.2.1 设置创建时间与修改时间
			vo.setCreated(LocalDateTime.now());
			vo.setUpdated(LocalDateTime.now());
			String password = vo.getPassword();
			//3.2.2 将密码加密
			vo.setPassword(SecureUtil.md5(password));
			//3.2.3 拷贝vo属性到user对象中
			User user = new User();
			BeanUtils.copyProperties(user,vo);
			//3.2.4 保存到数据库中
			userService.save(user);
		}
		return "register";
	}

	//4. 远程保存用户（分布式事务演示）
	@PostMapping("/user/save2")
	@ResponseBody
	public AjaxResult saveUser(){
		userService.save2();
		return AjaxResult.success();
	}

	//5. 查询所有用户
	@GetMapping("/user/list")
	@ResponseBody
	public R<String> list(){
		//5.1 定义资源的方法一：
		// String message = userService.getMessage1();
		//5.2 定义资源的方法二：
		String message = userService.getMessage2();
		return R.ok(message);
	}
	@GetMapping("/user/list2")
	@ResponseBody
	public R<String> list2(){

		return R.ok();
	}
	@GetMapping("/user/findAll")
	@ResponseBody
	AjaxResult findAll(){
		return AjaxResult.success("查询用户列表成功！");
	}
	//1. 演示分布式锁: 方法一（redis + Lua脚本）
	@GetMapping("/user/findAll2")
	@ResponseBody
	AjaxResult findAll2(){
		List<User> users = userService.findAll();
		return AjaxResult.success(users);
	}
	//2. 演示分布式锁: 方法二（redission框架）
	@GetMapping("/user/findAll3")
	@ResponseBody
	AjaxResult findAll3(){
		List<User> users = userService.findAll2();
		return AjaxResult.success(users);
	}


	@GetMapping("/user/message1")
	@ResponseBody
	public String message1(){
		userService.getMessage3();
		return "message1";
	}

	@GetMapping("/user/message2")
	@ResponseBody
	public String message2(){
		userService.getMessage3();
		return "message2";
	}

	//3. 降级处理: 每隔1s发一个请求
	@GetMapping(value = "/user/testB")
	@ResponseBody
	public String testB() throws InterruptedException {
		TimeUnit.SECONDS.sleep(1);
		return "testB";
	}

	@GetMapping(value = "/user/testD")
	@ResponseBody
	public String testD(int id){
		if(id<0){
			throw new RuntimeException("运行异常");
		}
		return "testD";
	}

}
